package tenant

import (
	"errors"
	"cgs-server/server"
	"cgs-server/server/base"
	"cgs-server/server/model"
	"go.mongodb.org/mongo-driver/bson"
)

type TenantRepository interface {
	base.Repository

	/*Insert(model interface{}) error
	Find(model interface{}) (interface{}, error)*/

	FindAll() (Tenants, error)
}

func NewTenantRepository() TenantRepository {
	return &tenantRepository{base.RepositoryStruct{
		CollectionName: server.Tenant,
	}}
}

type tenantRepository struct {
	base.RepositoryStruct
}

func (t *tenantRepository) Insert(duModel *model.DataUserModel) error {
	if err := t.CheckDB(); err != nil {
		return err
	}
	tenant := duModel.Data.(*Tenant)
	tenant.ResolveEntity(tenant, duModel.User)

	_, err := t.Mongo.InsertOne(t.CollectionName, tenant)
	return err
}

func (t *tenantRepository) Find(mdl *model.DataUserModel) (interface{}, error) {
	if err := t.CheckDB(); err != nil {
		return nil, err
	}
	var results = &Tenants{}
	//tenant := mdl.Data.(*Tenant)
	filter := bson.M{
		"$or": bson.A{
			bson.M{
				"isDeleted": 0,
			},
			bson.M{
				"isDeleted": bson.M{
					"$exists": false,
				},
			},
		},
	}
	//tenant.ResolveFilter(tenant, mdl.User, &filter)
	err := t.Mongo.FindMany(t.CollectionName, filter, results)
	if err != nil {
		return nil, err
	}
	//results.TimeUtcToCst()
	return results, nil
}

func (t *tenantRepository) FindOne(mdl *model.DataUserModel) (interface{}, error) {
	if err := t.CheckDB(); err != nil {
		return nil, err
	}
	var result Tenant
	tenant := mdl.Data.(*Tenant)
	filter := bson.M{
		"tenantId": tenant.TenantId,
	}
	tenant.ResolveFilter(tenant, mdl.User, &filter)
	if mdl.Filter != nil {
		filter = *mdl.Filter
	}
	find, err := t.Mongo.FindOne(t.CollectionName, filter, &result)
	if err != nil {
		return nil, err
	}
	if find {
		//result.TimeUtcToCst()
		return &result, nil
	}
	return nil, errors.New("not found")
}

func (t *tenantRepository) FindAll() (Tenants, error) {
	if err := t.CheckDB(); err != nil {
		return nil, err
	}
	filter := bson.M{
		"isDeleted": 0,
	}
	var results Tenants
	err := t.Mongo.FindMany(t.CollectionName, filter, &results)
	return results, err
}

func (t *tenantRepository) Update(mdl *model.DataUserModel) error {
	if err := t.CheckDB(); err != nil {
		return err
	}
	tenant := mdl.Data.(*Tenant)
	filter := bson.M{
		"tenantId": tenant.TenantId,
	}
	tenant.ResolveFilter(tenant, mdl.User, &filter)
	updateOpt := bson.M{
		"tenantName": tenant.TenantName,
	}
	tenant.ResolveUpdateCondition(tenant, mdl.User, &updateOpt)
	update := bson.M{
		"$set": updateOpt,
	}

	_, err := t.Mongo.UpdateOne(t.CollectionName, filter, update)
	return err
}

func (t *tenantRepository) DeleteLogic(mdl *model.DataUserModel) error {
	if err := t.CheckDB(); err != nil {
		return err
	}
	tenant := mdl.Data.(*Tenant)
	filter := bson.M{
		"tenantId": tenant.TenantId,
	}
	tenant.ResolveFilter(tenant, mdl.User, &filter)
	updateOpt := bson.M{}
	tenant.DeleteLogic(tenant, mdl.User, &updateOpt)
	update := bson.M{
		"$set": updateOpt,
	}

	_, err := t.Mongo.UpdateOne(t.CollectionName, filter, update)
	return err
}
